package com.example.myapplication.Jobsearch;

import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.widget.SearchView;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.example.myapplication.Config.OKHttpTool;
import com.example.myapplication.Config.RunOnUiThreadTool;
import com.example.myapplication.Config.ViewapteTool;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.Banner;
import com.example.myapplication.ShuJu.HotjobBean;
import com.example.myapplication.apter.HotjobApter;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class JobFragment extends Fragment {


    private View view;
    private ViewPager luimg;
    private SearchView sear;
    private TextView textsear;
    private String TAG = "TAG";
    private TabLayout mytab;
    private RecyclerView allpositions;
    private RecyclerView hotjob;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_job, container, false);
        initview();
        //轮播图
        Carouselmap();
        //热门职位
        hotjob();
        //全部职位
        allpositions();
        return view;
    }

    private void allpositions() {
        OKHttpTool.doNetRequest("prod-api/api/job/post/list" ,
                OKHttpTool.GET,
                HotjobBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        RunOnUiThreadTool.run(JobFragment.this.getActivity(), new RunOnUiThreadTool.RunCallback() {
                            @Override
                            public void set() {
                                HotjobBean hotjobBean1 = (HotjobBean)obj;
                                if(hotjobBean1.getCode()==200){
                                    allpositions.setLayoutManager(new LinearLayoutManager(JobFragment.this.getContext()));
                                    allpositions.setAdapter(new HotjobApter(JobFragment.this.getContext(),hotjobBean1.getRows()));
                                }
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void hotjob() {
        OKHttpTool.doNetRequest("prod-api/api/job/post/list", OKHttpTool.GET,
                HotjobBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        RunOnUiThreadTool.run(JobFragment.this.getActivity(), new RunOnUiThreadTool.RunCallback() {
                            @RequiresApi(api = Build.VERSION_CODES.N)
                            @Override
                            public void set() {
                                HotjobBean hotjobBean = (HotjobBean)obj;
                                if(hotjobBean.getCode()==200){
                                    List<HotjobBean.RowsBean> rows = hotjobBean.getRows();
                                    ArrayList<String> list = new ArrayList<>();
                                    for (HotjobBean.RowsBean row : rows) {
                                        list.add(row.getName());
                                    }
                                    HashMap<String, Integer> stringIntegerHashMap = new HashMap<>();
                                    for (String s : list) {
                                        if(stringIntegerHashMap.containsKey(s)){
                                            int count = stringIntegerHashMap.get(s);
                                            stringIntegerHashMap.put(s,count+1);
                                        }else{
                                            stringIntegerHashMap.put(s,1);
                                        }
                                    }

                                    List<Map.Entry<String, Integer>> sortedList = new ArrayList<>(stringIntegerHashMap.entrySet());
                                    sortedList.sort(Map.Entry.comparingByValue(Comparator.reverseOrder()));

                                    // 创建一个新的有序Map来存储排序后的结果
                                    Map<String, Integer> sortedMap = new LinkedHashMap<>();
                                    for (Map.Entry<String, Integer> entry : sortedList) {
                                        sortedMap.put(entry.getKey(), entry.getValue());
                                    }

                                    // 获取排序后的值列表
                                    List<String> sortedValues = new ArrayList<>(sortedMap.keySet());
                                   // sendgprofessionname(sortedValues.get(0));
                                   // Log.e(TAG, sortedValues.toString());
                                    for (int i = 0; i <3; i++) {
                                        TabLayout.Tab tab = mytab.newTab();
                                        tab.setText(sortedValues.get(i));
                                        mytab.addTab(tab);
                                    }

                                    sendhotjob(hotjobBean);
                                    //re.setLayoutManager(new GridLayoutManager(JobFragment.this.getContext(),1,GridLayoutManager.HORIZONTAL,false));
                                    // re.setAdapter(new HotjobApter(JobFragment.this.getActivity(),stringIntegerHashMap));
                                }
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void sendhotjob(HotjobBean hotjobBean) {
        mytab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                List<HotjobBean.RowsBean> rows = hotjobBean.getRows();
                String name = tab.getText().toString();
                for (HotjobBean.RowsBean row : rows) {
                    if(name.equals(row.getName())){
                        sendgprofessionname(name);
                        allpositions.setVisibility(View.GONE);
                    }
                }

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

    }

    private void sendgprofessionname(String name ) {
        OKHttpTool.doNetRequest("prod-api/api/job/post/list?name=" + name,
                OKHttpTool.GET,
                HotjobBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        RunOnUiThreadTool.run(JobFragment.this.getActivity(), new RunOnUiThreadTool.RunCallback() {
                            @Override
                            public void set() {
                                HotjobBean hotjobBean1 = (HotjobBean)obj;
                                if(hotjobBean1.getCode()==200){
                                    hotjob.setLayoutManager(new LinearLayoutManager(JobFragment.this.getContext()));
                                    hotjob.setAdapter(new HotjobApter(JobFragment.this.getContext(),hotjobBean1.getRows(),name));
                                }

                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });

    }

    private void Carouselmap() {
        OKHttpTool.doNetRequest("prod-api/api/rotation/list",
                OKHttpTool.GET,
                Banner.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {

                        RunOnUiThreadTool.run(JobFragment.this.getActivity(), new RunOnUiThreadTool.RunCallback() {
                            @Override
                            public void set() {
                                Banner  banner = (Banner)obj;
                                ArrayList<String> list = new ArrayList<>();
                                for (int i = 0; i < banner.getRows().size(); i++) {
                                    list.add(banner.getRows().get(i).getAdvImg());
                                }
                                ViewapteTool viewapteTool = new ViewapteTool(JobFragment.this.getActivity(),list);
                                luimg.setAdapter(viewapteTool);
                                mHandler.sendEmptyMessageDelayed(0, 3 * 1000);
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                }
        );
    }

    private void initview() {
        allpositions = view.findViewById(R.id.allpositions);
        hotjob = view.findViewById(R.id.hotjob);
        luimg = view.findViewById(R.id.luimg);
        textsear = view.findViewById(R.id.textsear);
        sear = view.findViewById(R.id.sear);
        mytab = view.findViewById(R.id.mytab);

    }


    private android.os.Handler mHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            if (msg.what == 0) {
                int index = luimg.getCurrentItem();
                index = (index + 1) % 3;
                luimg.setCurrentItem(index);
                mHandler.sendEmptyMessageDelayed(0, 3 * 1000);
            }
        }
    };
}